import sqlalchemy
# print(sqlalchemy.__version__)


from sqlalchemy import create_engine
engine = create_engine('sqlite:///sqlalchemy/alchemy.db', echo=True)

#Classes mapped using the Declarative system are defined in terms of a base class which maintains a catalog of classes and tables relative to that base -
#this is known as the declarative base class. Our application will usually have just one instance of this base in a commonly imported module.
#We create the base class using the declarative_base() function, as follows:
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


from sqlalchemy import Column, Integer, String

from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(32))
    fullname = Column(String(32))
    password = Column(String(32))

    def __repr__(self):
        return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password)


class Address(Base):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    email_address = Column(String, nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship("User", back_populates="addresses")
    def __repr__(self):
        return "<Address(email_address='%s')>" % self.email_address

User.addresses = relationship("Address", order_by=Address.id, back_populates="user")

from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
